//
//  DNStoolManager.m
//  DanoneEA
//
//  Created by Senela on 16/10/19.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//

#import "DNStoolManager.h"
#import "DNStoolDao.h"

@implementation DNStoolManager

#pragma mark - Public methods

+ (instancetype)sharedInstance {
    static DNStoolManager *sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}



-(BOOL)saveDairyToLocalDB:(DNStoolModel *)diaryInfo
{
    BOOL ret = NO;
    
    DNStoolDao *dao = [[DNStoolDao alloc] init];
    ret = [dao saveStoolModel:diaryInfo];
    
    return ret;
}

/**
 *  save stool dairy list in local db
 *
 *  @param diaryArr
 *
 *  @return YES/NO
 */
-(BOOL)saveStoolDairyArrToLocalDB:(NSArray *)diaryArr
{
    DNStoolDao *dao = [[DNStoolDao alloc] init];
    BOOL ret = [dao saveStoolModelWithArray:diaryArr];
    
    return ret;
}

-(BOOL)deleteDairyFromLocalDBByAppId:(NSString *)appId
{
    BOOL ret = NO;
    
    DNStoolDao *dao = [[DNStoolDao alloc] init];
    ret = [dao deleteStoolModelByAppId:appId];
    return ret;
}


-(void)synchronousSpittingData
{
    //                      NSDate *lastFourDate =[self getLastFourDate];
    
    // get All Local Stool data witch status is  OPStatus_ADD/OPStatus_MODIFY/OPStatus_DELETE From local DB by lastFourDate
    DNStoolDao *dao = [[DNStoolDao alloc] init];
    NSMutableArray *localStoolArr = [dao getAllObjectModelNotSynchronized];
    
    if(localStoolArr.count == 0)
        return;
    
    NSArray *dictArray = [DNStoolModel mj_keyValuesArrayWithObjectArray:localStoolArr];
    
    //TODO: upload data to application server and overwrite local data if succussed
    /*
     1、upload dictArray data to application server
     2、if failed， do nothing
     3、if succussed，update localCryingArr by serverId and status[OPStatus_NORMAL]
     4、save diaryArr info in local DB
     */
    [self saveDairyArrToLolcalDB:localStoolArr];
}


#pragma mark - Private methods
/**
 *  save diaryArr info in local DB without any modify
 *
 *  @param diaryArr <DNStoolModel>
 *
 *  @return YES/NO
 */
-(BOOL)saveDairyArrToLolcalDB:(NSArray*)diaryArr
{
    BOOL ret = NO;
    //TODO:save diaryArr info in local DB without any modify
    
    DNStoolDao *dao = [[DNStoolDao alloc] init];
    ret = [dao saveStoolModelWithArray:diaryArr];
    
    return ret;
}

@end
